angular.module('app')
    .directive('uploadpic', [function() {
        return {
            restrict: 'E',
            templateUrl:'tpl/upload/uploadpic.html',
            replace:true,
            scope:{
                app: '=',
                info:'=',
                queue:'=',//返回给controller交互数据
                queueLimit:'@',//由父controller限制数量
                path:'@',
                images:'=',//已保存图片
                delFunc:'=',
                unique:'='
            },
            controller: function ($scope,$window,FileUploader,$cookies,$stateParams,$q,toaster,utilService) {
                //上传对象
                var uploader = $scope.uploader = new FileUploader({
                    url: $scope.app.domain+'/upload',
                    formData:[{sessionId:$cookies.sessionId}]
                });

                // 过滤器
                uploader.filters.push({
                    name: 'picFilter',
                    fn: function(item, options) {
                        if(!(item.type=='image/png' ||item.type=='image/gif' || item.type=='image/jpeg' || item.type=='image/jpg')){
                            toaster.pop('waiting', "添加图片失败", "不属于图片类型");
                            return false;
                        }
                        if(item.size>2*1024*1024){
                            toaster.pop('waiting', "添加图片失败", "文件太大,超出2M大小");
                            return false;
                        }
                        if(this.queue.length > $scope.queueLimit){
                            toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
                            return false;
                        }
                        return true;
                    }
                });


                // 回调函数
                uploader.onWhenAddingFileFailed = function(item,filter, options) {
                    console.info('添加文件失败',item,filter, options);
                };
                uploader.onAfterAddingFile = function(fileItem) {
                    if($scope.queueLimit<uploader.queue.length){
                        toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
                        uploader.queue.splice(uploader.queue.indexOf(fileItem),1);
                    }
                };

                uploader.onAfterAddingAll = function(addedFileItems) {
                    if(!uploader.getNotUploadedItems().length){
                        return;
                    }
                    uploader.uploadAll();
                };
                uploader.onSuccessItem = function(fileItem, response, status, headers) {
                    switch (status){
                        case 200:
                            fileItem.fileName=response.data;
                            $scope.queue=$scope.uploader.queue;
                            if($scope.uploader.queue.length>0){
                                $scope.unique=$scope.uploader.queue[0].fileName;
                            }
                            break;
                        default :
                            break;
                    }
                };


                //移除临时图片
                $scope.del=function(item){
                    utilService.api({
                        url:$scope.app.domain + 'upload/',
                        data:{filefullPath:item.fileName},
                        method:'DELETE'
                    }).then(function(result){
                        switch (result.status){
                            case 200:
                                item.remove();
                                $scope.queue=uploader.queue;
                                break;
                            default :
                                break;
                        }
                    });
                }


                //移除上传失败图片
                $scope.delError=function(item){
                    item.remove();
                }

                //打开图片
                $scope.openUrl=function(url){
                    $window.open(url);
                }


            }
        };
    }])